package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.enums.AddressDefault;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.exception.PlatformException;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.platform.api.domain.PlatformAddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.platfom.PlatformAddressMapper;
import com.probox.common.entity.domain.platfom.PlatformAddress;
import com.probox.common.entity.service.platfom.IPlatformAddressService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺地址Service业务层处理
 *
 * @author usoft
 * @date 2021-07-05
 */
@Service
@Transactional
public class PlatformAddressServiceImpl extends ServiceImpl<PlatformAddressMapper, PlatformAddress> implements IPlatformAddressService {
    @Autowired
    private PlatformAddressMapper platformAddressMapper;


    /**
     * 查询店铺地址
     *
     * @param platformAddressUid 店铺地址ID
     * @return 店铺地址
     */
    @Override
    public PlatformAddress selectPlatformAddressById(Long platformAddressUid) {
        return this.getById(platformAddressUid);
    }

    /**
     * 查询店铺地址列表
     *
     * @param platformAddress 店铺地址
     * @return 店铺地址
     */
    @Override
    public List<PlatformAddress> queryList(PlatformAddress platformAddress) {
        QueryWrapper<PlatformAddress> queryWrapper = Wrappers.query();
        Long platformId = SecurityUtils.getPlatformId();
        if(platformId == null){
            throw new BaseException("当前账号异常");
        }
        queryWrapper.eq("PLATFORM_UID", platformId);
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(platformAddress.getName())) && platformAddress.getName() != null) {
            queryWrapper.eq("NAME", platformAddress.getName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformAddress.getMobile())) && platformAddress.getMobile() != null) {
            queryWrapper.eq("MOBILE", platformAddress.getMobile());
        }


        if (StringUtils.isNotEmpty(String.valueOf(platformAddress.getIsDefault())) && platformAddress.getIsDefault() != null) {
            queryWrapper.eq("IS_DEFAULT", platformAddress.getIsDefault());
        }

        if (StringUtils.isNotEmpty(String.valueOf(platformAddress.getDataStatus())) && platformAddress.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", platformAddress.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformAddress.getDataType())) && platformAddress.getDataType() != null) {
            queryWrapper.eq("DATA_TYPE", platformAddress.getDataType());
        }
        queryWrapper.orderByDesc("update_time");
        return platformAddressMapper.selectList(queryWrapper);
    }

    /**
     * 新增店铺地址
     *
     * @param platformAddress 店铺地址
     * @return 结果
     */
    @Override
    public boolean insertPlatformAddress(PlatformAddress platformAddress) {
        if(AddressDefault.tolerant.getKey().equals(platformAddress.getIsDefault())){
            updateIsDefulat(platformAddress.getDataType());
        }
        Long userId = SecurityUtils.getUserId();
        DateTime date = DateUtil.date();
        String username = SecurityUtils.getUsername();
        platformAddress.setPlatformUid(SecurityUtils.getPlatformId());
        platformAddress.setCreateUid(userId);
        platformAddress.setCreateName(username);
        platformAddress.setCreateTime(date);
        platformAddress.setUpdateUid(userId);
        platformAddress.setUpdateName(username);
        platformAddress.setUpdateTime(date);
        platformAddress.setDataStatus(DataStatus.valid.getKey());
        return this.save(platformAddress);
    }

    /**
     * 修改店铺地址
     *
     * @param platformAddress 店铺地址
     * @return 结果
     */
    @Override
    public boolean updatePlatformAddress(PlatformAddress platformAddress) {
        if(AddressDefault.tolerant.getKey().equals(platformAddress.getIsDefault())){
            updateIsDefulat(platformAddress.getDataType());
        }
        platformAddress.setUpdateUid(SecurityUtils.getUserId());
        platformAddress.setUpdateName(SecurityUtils.getUsername());
        platformAddress.setUpdateTime(DateUtil.date());
        return this.updateById(platformAddress);
    }

    /**
     * 批量删除店铺地址
     *
     * @param platformAddressUids 需要删除的店铺地址ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformAddressByIds(Long[] platformAddressUids) {
        UpdateWrapper<PlatformAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("PLATFORM_ADDRESS_UID", Arrays.asList(platformAddressUids));
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    /**
     *  将其他的地址设置为非默认
     * @param type
     */
    private void updateIsDefulat(String type){
        UpdateWrapper<PlatformAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("DATA_TYPE", type);
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        updateWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("IS_DEFAULT", AddressDefault.notTolerant.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        this.update(updateWrapper);
    }

    /**
     *  获取默认地址
     * @param vo
     * @return
     */
    @Override
    public PlatformAddressVo getAddress(PlatformAddressVo vo){
        QueryWrapper<PlatformAddress> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isEmpty(vo.getPlatformAddressUid())){
            queryWrapper.eq("PLATFORM_UID", vo.getPlatformUid());
            queryWrapper.eq("DATA_TYPE", vo.getDataType());
            queryWrapper.eq("IS_DEFAULT", AddressDefault.tolerant.getKey());
        }else{
            queryWrapper.eq("PLATFORM_ADDRESS_UID", vo.getPlatformUid());
        }
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        PlatformAddress platformAddress = platformAddressMapper.selectOne(queryWrapper);
        if(ObjectUtil.isEmpty(platformAddress)) return vo;
        BeanUtils.copyProperties(platformAddress, vo);
        return vo;
    }

}
